﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using SimpleUtils;
using SimpleUtils.Win;

namespace SimpleReminder
{
    public abstract class EventBase
    {
        #region Properties
        public string Id
        {
            get { return _dr[MyEvents.COL_ID] as string; }
        }

        public string Name
        {
            get { return _dr[MyEvents.COL_NAME] as string; }
        }

        public string Description
        {
            get { return _dr[MyEvents.COL_DESCRIPTION] as string; }
        }

        public MyEvents.EventTypes Type
        {
            get { return Utils.ParseEventType(_dr[MyEvents.COL_TYPE] as string); }
        }

        private bool _statusChanged = false;
        public bool StatusChanged
        {
            get { return _statusChanged; }
            protected set { _statusChanged = value; }
        }

        public MyEvents.EventStatus Status
        {
            get { return Utils.ParseEventStatus(_dr[MyEvents.COL_STATUS] as string); }
            set
            {
                string s = value.ToString();
                if (!_dr[MyEvents.COL_STATUS].Equals(s))
                {
                    _dr[MyEvents.COL_STATUS] = s;
                    StatusChanged = true;
                }
            }
        }

        public DateTime StartDate
        {
            get { return (DateTime) _dr[MyEvents.COL_STARTDATE]; }
        }

        public DateTime EndDate
        {
            get { return (DateTime)_dr[MyEvents.COL_ENDDATE]; }
        }

        public DateTime LastRunTime
        {
            get
            {
                DateTime dt = (DateTime)_dr[MyEvents.COL_LASTRUNTIME];
                if (dt > DateTime.Now)
                {
                    dt = System.Windows.Forms.DateTimePicker.MinimumDateTime;
                }
                return dt;
            }
            set
            {
                _dr[MyEvents.COL_LASTRUNTIME] = value;
            }
        }

        public DateTime LastLastRunTime
        {
            get { return (DateTime)_dr[MyEvents.COL_LASTLASTRUNTIME]; }
            set
            {
                _dr[MyEvents.COL_LASTLASTRUNTIME] = value;
            }
        }

        public string Reminder
        {
            get { return _dr[MyEvents.COL_REMINDER] as string; }
        }

        public int Frequence
        {
            get { return (int) _dr[MyEvents.COL_FREQUENCE]; }
        }

        public string AgentPath
        {
            get { return _dr[MyEvents.COL_AGENT_PATH] as string; }
        }
        public short AgentTop
        {
            get { return (short)(int)_dr[MyEvents.COL_AGENT_TOP]; }
        }
        public short AgentLeft
        {
            get { return (short)(int)_dr[MyEvents.COL_AGENT_LEFT]; }
        }
        public string AgentAnimation
        {
            get { return _dr[MyEvents.COL_AGENT_ANIMATION] as string; }
        }

        public string AppPath
        {
            get { return _dr[MyEvents.COL_APP_PATH] as string; }
        }

        public string AppArguments
        {
            get { return _dr[MyEvents.COL_APP_ARG] as string; }
        }

        public string EmailTo
        {
            get { return _dr[MyEvents.COL_EMAIL_TO] as string; }
        }

        public string EmailCc
        {
            get { return _dr[MyEvents.COL_EMAIL_CC] as string; }
        }

        public string EmailBcc
        {
            get { return _dr[MyEvents.COL_EMAIL_BCC] as string; }
        }

        #endregion Properties

        public EventBase(DataRow dr)
        {
            this.From(dr);
        }

        private DataRow _dr = null;

        //private void EndEdit()
        //{
        //    _dr.EndEdit();
        //}

        public virtual void From(DataRow dr)
        {
            _dr = dr;
        }

        public bool CanRun
        {
            get
            {
                DateTime now = DateTime.Now;
                if ( _dr.RowState == DataRowState.Deleted ||
                    Status != MyEvents.EventStatus.Active                     
                    )
                    return false;

                if (StartDate <= now && now <= EndDate &&
                    LastRunTime < now)
                    return true;

                return false;
            }
        }

        public virtual bool IsTimeToRun()
        {
            return false;
        }

        public virtual void Run()
        {
            DateTime now = DateTime.Now;
            LastLastRunTime = LastRunTime;
            LastRunTime = now;

            if (EndDate <= now)
            {
                Status = MyEvents.EventStatus.Completed;
            }
            _dr.EndEdit();

            switch (Reminder)
            {
                case MyEvents.REMINDER_AGENT:
                    RunAgentEvent();
                    break;
                case MyEvents.REMINDER_EMAIL:
                    RunEmailEvent();
                    break;
                case MyEvents.REMINDER_APP:
                    RunAppEvent();
                    break;
            }

        }

        private void RunAgentEvent()
        {
            Program.MainForm.RunAgent(Id, AgentPath, AgentTop, AgentLeft, AgentAnimation, Description);
        }

        private void RunEmailEvent()
        {
            Utils.SendEmail(EmailTo, EmailCc, EmailBcc, Name, Description);
        }

        private void RunAppEvent()
        {
            if(!String.IsNullOrEmpty(AppPath))
                SimpleProcess.Start(AppPath, AppArguments);
        }

    }//end of class
}
